package com.dhkj.circle.utils;

import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;

import com.dhkj.circle.broadcast.CardEvent;
import com.dhkj.circle.constant.SystemConst;
import com.dhkj.circle.entity.CardInfo;
import com.language.gb.GbEnDecode;

import org.greenrobot.eventbus.EventBus;

import java.io.EOFException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Stack;

import static com.dhkj.circle.ui.AppContext.sLogger;
import static com.dhkj.circle.utils.StringUtils.*;


/**
 * NFC的节本的读写以及解码的工具类
 */
public class NFCUtils {
    //将nfc的实体进行全局初始化
    private static MifareClassic mfc = null;

    private static CardInfo cardInfo;

    private static Stack<String> s = new Stack<String>();

    public static CardInfo getCardInfo() {
        return cardInfo;
    }

    public static void setCardInfo(CardInfo cardInfo) {
        NFCUtils.cardInfo = cardInfo;
    }

    private static boolean pr = false;
    private static HashMap<String, Intent> mIntentMap = new HashMap<>();
    private static HashMap<String, Tag> mTagMap = new HashMap<>();

    public static void scanNfc(Intent intent, String mName) {
        try {
            mIntentMap.put(mName, intent);
            if (s.isEmpty() || !mName.equals(s.peek())) {
                s.push(mName);
            }
            EventBus.getDefault().post(new CardEvent(0, mName));

        } catch (Exception e) {
            sLogger.e(e.getMessage());
            EventBus.getDefault().post(new CardEvent(1, mName));
        }
    }

    public static void remove(String mName) {
        mIntentMap.remove(mName);
    }

    public static int readNFC(String mName) {
        Intent intent = mIntentMap.get(mName);
        if (intent != null) {
            try {
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                nfcInit(tag);
                if (readTag2() == 1) {
                    return 10;
                }
                readTag();
                mfc.close();
            } catch (Exception e) {
//                sLogger.d(e.getMessage());
                return 3;
            }

        } else {
            Intent intent2 = null;
            do {
                if (s.isEmpty()) {
                    return 2;
                }
                String name = s.peek();
                intent2 = mIntentMap.get(name);
                if (intent2 == null) {
                    s.pop();
                }
            } while (intent2 == null);
            try {
                Tag tag = intent2.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                nfcInit(tag);
                if (readTag2() == 1) {
                    return 10;
                }
                readTag();
                mfc.close();
            } catch (Exception e) {
//                sLogger.d(e.getMessage());
                return 3;
            }
        }
        return 1;
    }

    public static int writeNFC(String mName, CardInfo cardInfo) throws Exception {
        NFCUtils.setCardInfo(cardInfo);
        Intent intent = mIntentMap.get(mName);
        if (intent != null) {
            try {
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                nfcInit(tag);
                WriteCardMoneyAll(cardInfo);
                mfc.close();
            } catch (Exception e) {
//                sLogger.d(e.getMessage());
                throw new Exception("写入错误");
            }
        } else {
            Intent intent2 = null;
            do {
                if (s.isEmpty()) {
                    throw new Exception("写入错误:找不到Intent");
                }
                String name = s.peek();
                intent2 = mIntentMap.get(name);
                if (intent2 == null) {
                    s.pop();
                }
            } while (intent2 == null);
            try {
                Tag tag = intent2.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                nfcInit(tag);
                WriteCardMoneyAll(cardInfo);
                mfc.close();
            } catch (Exception e) {
//                sLogger.d(e.getMessage());
                throw new Exception("写入错误:写Intent2");
            }
        }
        return 1;
    }

    //NFC初始化
    public static int nfcInit(Tag tag) {
        try {
            if (mfc != null && mfc.isConnected()) {
                mfc.close();
            }
            mfc = MifareClassic.get(tag);

            mfc.connect();
        } catch (Exception e) {
            sLogger.e(e.getMessage());
        }
        return 1;
    }

    public static void close() {
        try {
            mfc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int readTag2() throws Exception {
        if (mfc == null) {
            throw new Exception("NFC错误");
        }
        try {
            mfc.authenticateSectorWithKeyA(4, SystemConst.NFC_DEFLUT_PASSWORD2);//取密码
            byte[] data1 = mfc.readBlock(16);
            return 1;
        } catch (Exception e) {
            if (pr) {
                sLogger.e(e.getMessage());
            }
        }
        return 0;
    }

    //读取数据
    public static void readTag() throws Exception {
        String str = "0";
        if (mfc == null) {
            throw new Exception("NFC错误");
        }
        try {
            mfc.authenticateSectorWithKeyA(2, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
            byte[] data1 = mfc.readBlock(8);
            byte[] data2 = mfc.readBlock(9);
            byte[] data3 = mfc.readBlock(10);
            mfc.authenticateSectorWithKeyA(3, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
            byte[] data4 = mfc.readBlock(12);
            CardInfo Cardinfo = new CardInfo();
            int i;
            byte tmpa;
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (data1[i] + tmpa);
            }
            tmpa = (byte) ~tmpa;
            if (tmpa != data1[15]) {
                str = "50";
                throw new Exception("校验数据出错50");
                //	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            }
            //	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (data2[i] + tmpa);
            }
            tmpa = (byte) ~tmpa;
            if (tmpa != data2[15]) {
                str = "51";
                throw new Exception("校验数据出错51");
//                	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            }
            //	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (data3[i] + tmpa);
            }
            tmpa = (byte) ~tmpa;

            if (tmpa != data3[15]) {
                str = "52";
                throw new Exception("校验数据出错52");
                //Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            }
            //	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (data4[i] + tmpa);
            }
            tmpa = (byte) ~tmpa;
            if (tmpa != data4[15]) {
                str = "60";
                throw new Exception("校验数据出错60");
                //		Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            }
            //	Status.GetInstance().WriteErrorFile("str:"+str+"tmpa:"+tmpa+"\r\n");
            Cardinfo.KL = (char) data1[0];
            int count = 0;

            byte[] k = new byte[4];
            for (int j = 1; j < 3; j++) {
                byte[] bDataget = HexToAscii(data1[j]);// 转码 写卡要转回来
                k[count] = bDataget[0];
                count++;
                k[count] = bDataget[1];
                count++;
            }
            String str2 = new String(k, 0, k.length);
            Cardinfo.dwmm = str2;

            Cardinfo.ver = (char) data1[3];
            for (i = 5; i < 15; i++) {
                Cardinfo.zyh[i - 5] = (char) data1[i];
            }
            String khby1 = bytesToHexString(data2[0]);
            String khby2 = bytesToHexString(data2[1]);
            String khby3 = bytesToHexString(data2[2]);
            int kha = Integer.parseInt(khby1, 16);
            int khb = Integer.parseInt(khby2, 16);// 涉及到金额都要转码
            int khc = Integer.parseInt(khby3, 16);
            Cardinfo.kh = kha * 0x10000 + khb * 0x100 + khc; // 金额转码 写卡要转回来
            Cardinfo.KCB = (int) data2[3];
            Cardinfo.BTBytes = data2[4];
            byte[] name = new byte[10];
            for (i = 5; i < 15; i++) {
                name[i - 5] = data2[i];
            }
            Cardinfo.name = GbEnDecode.GB2312ToUCS2(name, 10);
            Cardinfo.ZH = (char) data3[0];
            Cardinfo.BQH[0] = (char) data3[1];
            Cardinfo.BQH[1] = (char) data3[2];
            Cardinfo.KSH[0] = (char) data3[3];
            Cardinfo.KSH[1] = (char) data3[4];
            Cardinfo.BED[0] = (char) data3[5];
            Cardinfo.BED[1] = (char) data3[6];
            Cardinfo.BED[2] = (char) data3[7];
            Cardinfo.BED[3] = (char) data3[8];
            Cardinfo.BED[4] = (char) data3[9];
            // Cardinfo.FKRQ[0]=(char)data3[10];
            // Cardinfo.FKRQ[1]=(char)data3[11];
            // Cardinfo.FKRQ[2]=(char)data3[12];
            int count1 = 0;
            byte[] kk = new byte[6];
            for (int j = 10; j < 13; j++) {
                byte[] bDataget = HexToAscii(data3[j]);// 转码 写卡要转回来
                kk[count1] = bDataget[0];
                count1++;
                kk[count1] = bDataget[1];
                count1++;
            }
            String str1 = new String(kk, 0, kk.length);
            Cardinfo.FKRQ = str1;
            // m_tfContent3.setString(Cardinfo.FKRQ);
            // m_siInfo.setText("------"+str1+"++"+str2);

            // String str3 = new String(data3,0,data3.length);
            Cardinfo.CardLost = data3[13];

            Cardinfo.CardStru = data4[0];
            String by1 = bytesToHexString(data4[2]);
            String by2 = bytesToHexString(data4[3]);
            String by3 = bytesToHexString(data4[4]);
            int a = Integer.parseInt(by1, 16);
            int b = Integer.parseInt(by2, 16);// 涉及到金额都要转码
            int c = Integer.parseInt(by3, 16);
            Cardinfo.je = a * 0x10000 + b * 0x100 + c; // 金额转码 写卡要转回来

            String btby1 = bytesToHexString(data4[5]);
            String btby2 = bytesToHexString(data4[6]);
            String btby3 = bytesToHexString(data4[7]);
            int bta = Integer.parseInt(btby1, 16);
            int btb = Integer.parseInt(btby2, 16);// 涉及到金额都要转码
            int btc = Integer.parseInt(btby3, 16);
            Cardinfo.btje = bta * 0x10000 + btb * 0x100 + btc;

            // Cardinfo.btje= (byte)(data4[5]) * 0x10000 + (byte)(data4[6]) * 0x100
            // +(byte)(data4[7]); // 金额转码 写卡要转回来

            String addby1 = bytesToHexString(data4[8]);
            String addby2 = bytesToHexString(data4[9]);
            String addby3 = bytesToHexString(data4[10]);
            int adda = Integer.parseInt(addby1, 16);
            int addb = Integer.parseInt(addby2, 16);// 涉及到金额都要转码
            int addc = Integer.parseInt(addby3, 16);
            Cardinfo.addje = adda * 0x10000 + addb * 0x100 + addc;
            // Cardinfo.addje= (byte)(data4[8]) * 0x10000 + (byte)(data4[9]) * 0x100
            // +(byte)(data4[10]); // 金额转码 写卡要转回来
            String timeby1 = bytesToHexString(data4[13]);
            String timeby2 = bytesToHexString(data4[14]);
            int timea = Integer.parseInt(timeby1, 16);
            int timeb = Integer.parseInt(timeby2, 16);// 涉及到金额都要转码
            Cardinfo.UseTimes = timea * 0x100 + timeb;
            // Cardinfo.UseTimes =(byte)(data4[13]) * 0x100 +(byte)(data4[14]);
            // 金额转码 写卡要转回来
            setCardInfo(Cardinfo);
        } catch (IOException e) {
//            sLogger.d(e.getMessage());
            throw new Exception("NFC读取错误");
        }
    }

    //进行写操作
    public void WriteCardMoney(CardInfo cardInfo) throws Exception {
        try {
            System.out.println("开始写卡");

            byte[] wdata3 = new byte[16];
            int i;
            for (i = 0; i < 16; i++) {
                wdata3[i] = 0x00;
            }
            wdata3[0] = cardInfo.CardStru;
            String je;
            je = Integer.toHexString(cardInfo.je); // 金额转码
            int jelen = je.length();
            if (jelen < 6) {
                for (i = 0; (i < 6 - jelen); i++) { // 补码
                    je = "0" + je;
                }
            }
            if (jelen > 6) {
                je = je.substring(jelen - 6, 6);// 取码
            }
            byte[] jeb = hexStringToBytes(je);
            wdata3[2] = jeb[0];
            wdata3[3] = jeb[1];
            wdata3[4] = jeb[2];
            String bt;
            bt = Integer.toHexString(cardInfo.btje);
            int btlen = bt.length();
            if (btlen < 6) {
                for (i = 0; (i < 6 - btlen); i++) {
                    bt = "0" + bt;
                }
            }
            // m_tfData2.setString("bt1-------"+bt);
            if (btlen > 6) {
                bt = bt.substring(btlen - 6, 6);
            }
            byte[] btb = hexStringToBytes(bt);
            wdata3[5] = btb[0];
            wdata3[6] = btb[1];
            wdata3[7] = btb[2];
            String add;
            add = Integer.toHexString(cardInfo.addje);
            int addlen = add.length();
            if (addlen < 6) {
                for (i = 0; (i < 6 - addlen); i++) {
                    add = "0" + add;
                }
            }
            if (addlen > 6) {
                add = add.substring(addlen - 6, 6);
            }
            byte[] addb = hexStringToBytes(add);
            wdata3[8] = addb[0];
            wdata3[9] = addb[1];
            wdata3[10] = addb[2];
            String use;
            use = Integer.toHexString(cardInfo.UseTimes);
            int uselen = use.length();
            if (uselen < 4) {
                for (i = 0; (i < 4 - uselen); i++) {
                    use = "0" + use;
                }
            }
            if (use.length() > 4) {
                use = use.substring(uselen - 4, 4);
            }
            byte[] useb = hexStringToBytes(use);
            wdata3[13] = useb[0];
            wdata3[14] = useb[1];
            byte tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (tmpa + wdata3[i]);
            }
            wdata3[15] = (byte) ~tmpa; // 校验数据
            // 6扇区1块v 金额
            try {
                mfc.authenticateSectorWithKeyA(6, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
                mfc.writeBlock(25, wdata3);
            } catch (Exception huayuE) {
                throw new Exception("写数据出错1 " + huayuE.getMessage());
            }
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
            // 6扇区0块 金额
            try {
                mfc.authenticateSectorWithKeyA(6, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
                mfc.writeBlock(24, wdata3);
            } catch (Exception huayuE) {
                throw new Exception("写数据出错0 " + huayuE.getMessage());
            }
            setCardInfo(cardInfo);
        } catch (EOFException e) {
            boolean CodeAuth = false;
            throw new Exception("写数据出错");
        }
    }

    //进行写操作
    public static void WriteCardMoneyAll(CardInfo cardInfo) throws Exception {
        try {
            cardInfo.UseTimes = cardInfo.UseTimes + 1;
            byte[] wdata = new byte[16];
            byte[] wdata1 = new byte[16];
            byte[] wdata2 = new byte[16];
            byte[] wdata3 = new byte[16];
            int i;
            for (i = 0; i < 16; i++) {
                wdata[i] = wdata1[i] = wdata2[i] = wdata3[i] = 0x00;
            }
            wdata[0] = (byte) cardInfo.KL;
            wdata[1] = (byte) AsciiToHex((byte) cardInfo.dwmm.charAt(0), (byte) cardInfo.dwmm.charAt(1));
            wdata[2] = (byte) AsciiToHex((byte) cardInfo.dwmm.charAt(2), (byte) cardInfo.dwmm.charAt(3));//转码
            wdata[3] = (byte) cardInfo.ver;
            for (i = 5; i < 15; i++) {
                try {
                    wdata[i] = (byte) cardInfo.zyh[i - 5];
                } catch (Exception e) {
                    throw new Exception("写数据出错");
                }
            }
            byte tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (tmpa + wdata[i]);
            }
            wdata[15] = (byte) ~tmpa;
            // wdata3[2]=hexStringToByte(je1);
            // wdata3[3]=hexStringToByte(je2);
            // wdata3[4]=hexStringToByte(je3);

            String kh;
            // kh1=Integer.toHexString(Cardinfo.kh/0x10000);
            // kh2=Integer.toHexString(Cardinfo.kh/0x100);
            kh = Integer.toHexString(cardInfo.kh);
            int khlen = kh.length();
            if (khlen < 6) {
                for (i = 0; (i < 6 - khlen); i++) {
                    kh = "0" + kh;
                }
                //m_siInfo.setText(String.valueOf(kh.length()));
            }
            //  m_tfData.setString("-----"+kh);
            if (khlen > 6) {
                kh = kh.substring(khlen - 6, 6);
            }
            //  m_siInfo.setText("-------------"+kh);
            byte[] kyb = hexStringToBytes(kh);
            wdata1[0] = (byte) kyb[0];
            wdata1[1] = (byte) kyb[1];   //卡号转码
            wdata1[2] = (byte) kyb[2];
            // wdata1[0]=(byte)typeConvert.AsciiToHex((byte)kh.charAt(0),(byte)kh.charAt(1));
            // wdata1[1]=(byte)typeConvert.AsciiToHex((byte)kh.charAt(2),(byte)kh.charAt(3));
            // wdata1[2]=(byte)typeConvert.AsciiToHex((byte)kh.charAt(4),(byte)kh.charAt(5));
            wdata1[3] = (byte) cardInfo.KCB;
            wdata1[4] = cardInfo.BTBytes;
            String str = cardInfo.name;
            byte[] byteP;
            //int len = str.length();
            byteP = GbEnDecode.UCS2ToGB2312(str);
            int ilen = byteP.length;
            //if(ilen != 10){

            //}
            for (i = 0; i < ilen; i++) {
                wdata1[i + 5] = (byte) byteP[i];
            }
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (tmpa + wdata1[i]);
            }
            wdata1[15] = (byte) ~tmpa;    //校验数据
            try {
                wdata2[0] = (byte) cardInfo.ZH;
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }
            try {
                wdata2[1] = (byte) cardInfo.BQH[0];
                wdata2[2] = (byte) cardInfo.BQH[1];
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }
            try {
                wdata2[3] = (byte) cardInfo.KSH[0];
                wdata2[4] = (byte) cardInfo.KSH[1];
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }
            try {
                wdata2[5] = (byte) cardInfo.BED[0];
                wdata2[6] = (byte) cardInfo.BED[1];
                wdata2[7] = (byte) cardInfo.BED[2];
                wdata2[8] = (byte) cardInfo.BED[3];
                wdata2[9] = (byte) cardInfo.BED[4];
            } catch (Exception e) {
                throw new Exception("写数据出错52");
            }
            try {
                wdata2[10] = (byte) AsciiToHex((byte) cardInfo.FKRQ.charAt(0), (byte) cardInfo.FKRQ.charAt(1));
                wdata2[11] = (byte) AsciiToHex((byte) cardInfo.FKRQ.charAt(2), (byte) cardInfo.FKRQ.charAt(3));
                wdata2[12] = (byte) AsciiToHex((byte) cardInfo.FKRQ.charAt(4), (byte) cardInfo.FKRQ.charAt(5));

            } catch (Exception e) {
                throw new Exception("写数据出错");
            }


            // m_tfData.setString("-----"+Cardinfo.FKRQ);

            wdata2[13] = cardInfo.CardLost;
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (tmpa + wdata2[i]);
            }
            wdata2[15] = (byte) ~tmpa;    //校验数据

            wdata3[0] = cardInfo.CardStru;

            String je;

            je = Integer.toHexString(cardInfo.je);   //金额转码
            int jelen = je.length();
            if (jelen < 6) {
                for (i = 0; (i < 6 - jelen); i++) {    //补码
                    je = "0" + je;

                }
            }

            if (jelen > 6) {
                je = je.substring(jelen - 6, 6);//取码
            }
            byte[] jeb = hexStringToBytes(je);
            try {
                wdata3[2] = jeb[0];
                wdata3[3] = jeb[1];
                wdata3[4] = jeb[2];
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }


            String bt;
            bt = Integer.toHexString(cardInfo.btje);
            int btlen = bt.length();
            if (btlen < 6) {
                for (i = 0; (i < 6 - btlen); i++) {
                    bt = "0" + bt;
                }
            }

            //  m_tfData2.setString("bt1-------"+bt);
            if (btlen > 6) {
                bt = bt.substring(btlen - 6, 6);
            }
            byte[] btb = hexStringToBytes(bt);
            try {
                wdata3[5] = btb[0];
                wdata3[6] = btb[1];
                wdata3[7] = btb[2];
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }

            String add;

            add = Integer.toHexString(cardInfo.addje);
            int addlen = add.length();
            if (addlen < 6) {
                for (i = 0; (i < 6 - addlen); i++) {
                    add = "0" + add;
                }
            }

            if (addlen > 6) {
                add = add.substring(addlen - 6, 6);
            }
            byte[] addb = hexStringToBytes(add);
            wdata3[8] = addb[0];
            wdata3[9] = addb[1];
            wdata3[10] = addb[2];
            //wdata3[8]=(byte)typeConvert.AsciiToHex((byte)add.charAt(0),(byte)add.charAt(1));
            //wdata3[9]=(byte)typeConvert.AsciiToHex((byte)add.charAt(2),(byte)add.charAt(3));
            // wdata3[10]=(byte)typeConvert.AsciiToHex((byte)add.charAt(4),(byte)add.charAt(5));

            String use;
            //use2=Integer.toHexString(Cardinfo.UseTimes/0x100);
            use = Integer.toHexString(cardInfo.UseTimes);
            int uselen = use.length();
            if (uselen < 4) {
                for (i = 0; (i < 4 - uselen); i++) {
                    use = "0" + use;
                }
            }
            if (use.length() > 4) {
                try {
                    use = use.substring(uselen - 4, 4);
                } catch (Exception e) {
                    throw new Exception("写数据出错");
                }
            }
            byte[] useb = hexStringToBytes(use);
            try {
                wdata3[13] = useb[0];
                wdata3[14] = useb[1];
            } catch (Exception e) {
                throw new Exception("写数据出错");
            }
            tmpa = 0x00;
            for (i = 0; i < 15; i++) {
                tmpa = (byte) (tmpa + wdata3[i]);
            }
            wdata3[15] = (byte) ~tmpa;    //校验数据
            try {
                mfc.authenticateSectorWithKeyA(2, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
                mfc.writeBlock(8, wdata);
                mfc.writeBlock(9, wdata1);
                mfc.writeBlock(10, wdata2);
            } catch (Exception huayuE) {
                throw new Exception("写数据出错1 " + huayuE.getMessage());
            }
            // 6扇区1块v 金额
            try {
                mfc.authenticateSectorWithKeyA(3, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
                mfc.writeBlock(12, wdata3);
            } catch (Exception huayuE) {
                throw new Exception("写数据出错1 " + huayuE.getMessage());
            }

            // 6扇区0块 金额
            try {
                mfc.authenticateSectorWithKeyA(6, SystemConst.NFC_DEFLUT_PASSWORD1);//取密码
                mfc.writeBlock(24, wdata3);
            } catch (Exception huayuE) {
                throw new Exception("写数据出错0 " + huayuE.getMessage());
            }
            setCardInfo(cardInfo);
        } catch (EOFException e) {
            boolean CodeAuth = false;
            throw new Exception("写数据出错");
        } catch (Exception e) {
            throw new Exception("写数据出错");
        }
    }
}
